#!/usr/bin/perl -w

####################################################
# Copyright (c) 2002-2007 BioWare Corp.
# http://www.bigdaddymonitor.org
#
# This file is part of the BigDaddy Monitoring
# program. It is free software distributed under
# the terms of the BSD License. It is ditributed
# in the hopes that it will be useful, but
# WITHOUT ANY WARRANTY; without event the implied
# warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
#
# The BSD License is available from:
# http://www.opensource.org/licenses/bsd-license.php
####################################################


# Author Dups <dups@dups.ca>
# 26.08.2007
#
# This script takes input and creates all the database tables for use in BigDaddy

BEGIN
{
	push(@INC,'/bigdaddy')
}

use strict;
use Config::IniFiles;
use BioMain qw(:allfunctions);
use DBI;

#Check to make sure that all is good
print "Script will create tables and initialize the BigDaddy Data Structure...\n";
print "You should only run this once.\n";
print "Checking to see if data tables exist yet...\n\n";

#===================================================================================================
#Before we do anything we are going to bring up the main ini file: bigdaddy.ini should be in the
#same dir as these executables.
my $bigdaddy_cfg= new Config::IniFiles( -file => "/bigdaddy/bigdaddy.ini" );

#Get the dbini handle
my $dbIniHandle = $bigdaddy_cfg->val('main', 'dbinihandle');

my $dbb = callfor_db_handle($dbIniHandle, 0, 99);

if (!defined($dbb))
{
    print "Application Database Handle Could Not Be Opened: Check Database Existence";
    die();
}

#Get the dbini handle
my $dbStatsHandle = $bigdaddy_cfg->val('main', 'historydbhandle');

my $dbs = callfor_db_handle($dbStatsHandle, 0, 99);

if (!defined($dbb))
{
    print "Stats Database Handle Could Not Be Opened: Check Database Existence";
    die();
}
#===================================================================================================


my $sql="SHOW TABLES";
my $result=$dbb->prepare( $sql );
$result->execute;

if ($result->rows > 0)
{
    print "Sorry there are tables already in the Application database, you can ONLY use this on an EMPTY database\n";
    die("Fatal Error: Exiting");
}


$result=$dbs->prepare( $sql );
$result->execute;

if ($result->rows > 0)
{
    print "Sorry there are tables already in the Stats database, you can ONLY use this on an EMPTY database\n";
    die("Fatal Error: Exiting");
}

print "Do you want to continue and create the database tables? (Y/N) > ";

chomp ( my $yes_no = <> );

$yes_no = lc($yes_no);

if ($yes_no ne "y" && $yes_no ne "yes")
{
    die();
}

print "\nIMPORTANT: If you interrupt please clear application and stats databases and try again\n\n[Press any key to continue]\n\n";

my $wait = <>;

$sql = << "END"

--
-- Table structure for table `auditData`
--

CREATE TABLE `auditData` (
  `ID` int(10) NOT NULL auto_increment,
  `application` varchar(100) NOT NULL default '',
  `modifiedDate` datetime default NULL,
  `userID` int(10) NOT NULL default '0',
  `userIDAffected` int(10) NOT NULL default '0',
  `username` varchar(40) NOT NULL default '',
  `usernameAffected` varchar(40) NOT NULL default '',
  `primaryNumericID` int(10) unsigned NOT NULL default '0',
  `secondaryNumericID` int(10) unsigned NOT NULL default '0',
  `primaryStringID` varchar(255) NOT NULL default '',
  `secondaryStringID` varchar(255) NOT NULL default '',
  `type` varchar(80) NOT NULL default '',
  `action` varchar(80) NOT NULL default '',
  `comment` text,
  PRIMARY KEY  (`ID`),
  KEY `application` (`application`),
  KEY `username` (`username`),
  KEY `usernameAffected` (`usernameAffected`),
  KEY `primaryNumericID` (`primaryNumericID`),
  KEY `primaryStringID` (`primaryStringID`),
  KEY `type` (`type`),
  KEY `action` (`action`),
  KEY `modifiedDate` (`modifiedDate`),
  KEY `userID` (`userID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.auditData...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `cron_category`
--

CREATE TABLE `cron_category` (
  `ID` mediumint(8) unsigned NOT NULL auto_increment,
  `name` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`ID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.cron_category...\n";


$result=$dbb->prepare( $sql );
$result->execute;

$sql = "INSERT INTO cron_category SET ID=1, name='BigDaddy Crons'";
$dbb->do( $sql );

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `cron_category_map`
--

CREATE TABLE `cron_category_map` (
  `cronID` int(8) unsigned NOT NULL default '0',
  `categoryID` mediumint(8) unsigned NOT NULL default '0',
  KEY `categoryID` (`categoryID`),
  KEY `cronID` (`cronID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.cron_category_map...\n";


$result=$dbb->prepare( $sql );
$result->execute;

$sql = "INSERT INTO cron_category_map VALUES (?,?)";
$result=$dbb->prepare( $sql );

$result->execute(1,1);
$result->execute(2,1);
$result->execute(3,1);
$result->execute(4,1);
$result->execute(5,1);
$result->execute(6,1);
$result->execute(7,1);
$result->execute(8,1);
$result->execute(9,1);

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `cronjob`
--

CREATE TABLE `cronjob` (
  `ID` int(10) NOT NULL auto_increment,
  `serverID` mediumint(8) unsigned NOT NULL default '0',
  `iniID` varchar(40) NOT NULL default '',
  `active` tinyint(1) NOT NULL default '0',
  `ignoreMonitor` tinyint(1) NOT NULL default '0',
  `filename` varchar(100) NOT NULL default '',
  `last_ranstart` int(10) unsigned NOT NULL default '0',
  `last_ranend` int(10) unsigned NOT NULL default '0',
  `last_ranenabled` int(10) unsigned NOT NULL default '0',
  `delayed_until` int(10) unsigned NOT NULL default '0',
  `notes` text,
  `mon_startlevel` tinyint(1) unsigned NOT NULL default '0',
  `mon_currentlevel` tinyint(1) unsigned NOT NULL default '0',
  `mon_currentlevelstart` int(10) unsigned NOT NULL default '0',
  `mon_lastcheck` int(10) unsigned NOT NULL default '0',
  `mon_endlevel` tinyint(1) unsigned NOT NULL default '0',
  `mon_threshold` mediumint(8) unsigned NOT NULL default '0',
  PRIMARY KEY  (`ID`),
  KEY `serverID` (`serverID`),
  KEY `active` (`active`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.cronjob...\n";

$result=$dbb->prepare( $sql );
$result->execute;

$sql = "INSERT INTO cronjob SET ID=?, active=?, filename=?";
$result=$dbb->prepare( $sql );

$result->execute(1,1,'statscron_bool15history.php');
$result->execute(2,1,'statscron_bool180history.php');
$result->execute(3,1,'statscron_bool60history.php');
$result->execute(4,1,'statscron_ma15.php');
$result->execute(5,1,'statscron_ma180.php');
$result->execute(6,1,'statscron_ma60.php');
$result->execute(7,1,'statscron_nonbool.php');
$result->execute(8,1,'servermon_delaycheck.pl');
$result->execute(9,1,'crontab_delaycheck.pl');

#==============================================================================================
$sql = << "END"
--
-- Table structure for table `incident`
--

CREATE TABLE `incident` (
  `ID` int(10) NOT NULL auto_increment,
  `incidentStart` int(10) unsigned NOT NULL default '0',
  `incidentEnd` int(10) unsigned NOT NULL default '0',
  `postedBy` varchar(40) NOT NULL default '',
  `postedTime` int(10) unsigned NOT NULL default '0',
  `title` varchar(150) NOT NULL default '',
  `description` text NOT NULL,
  `reasons` text NOT NULL,
  `followup` text NOT NULL,
  PRIMARY KEY  (`ID`),
  KEY `postedTime` (`postedTime`)
) ENGINE=InnoDB;
END
;

print "Creating Table bigdaddy.incident...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"
--
-- Table structure for table `incidentServer`
--

CREATE TABLE `incidentServer` (
  `incidentID` int(10) unsigned NOT NULL default '0',
  `serverID` int(10) unsigned NOT NULL default '0',
  KEY `incidentID` (`incidentID`),
  KEY `serverID` (`serverID`)
) ENGINE=InnoDB;
END
;

print "Creating Table bigdaddy.incidentServer...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `phoneTag`
--

CREATE TABLE `phoneTag` (
  `ID` varchar(30) NOT NULL,
  `phoneNumber` varchar(150) NOT NULL default '',
  `notes` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`ID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.phoneTag...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `quicklinks`
--

CREATE TABLE `quicklinks` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `username` varchar(50) NOT NULL default '',
  `linkname` varchar(50) NOT NULL default '',
  `linkurl` varchar(255) NOT NULL default '',
  `ord` tinyint(3) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB;
END
;

print "Creating Table bigdaddy.quicklinks...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `report`
--

CREATE TABLE `report` (
  `ID` varchar(40) NOT NULL default '',
  `processID` varchar(40) NOT NULL default '',
  `report` int(10) unsigned NOT NULL default '0',
  `action` tinyint(1) unsigned NOT NULL default '0',
  PRIMARY KEY  (`ID`)
) ENGINE=InnoDB;
END
;

print "Creating Table bigdaddy.report...\n";


$result=$dbb->prepare( $sql );
$result->execute;

$sql = "INSERT INTO report SET ID = 'servermon'";
$dbb->do($sql);
$sql = "INSERT INTO report SET ID = 'servergroupmon'";
$dbb->do($sql);
$sql = "INSERT INTO report SET ID = 'excheck'";
$dbb->do($sql);
$sql = "INSERT INTO report SET ID = 'crontabmon'";
$dbb->do($sql);

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `report_group`
--

CREATE TABLE `report_group` (
  `ID` int(10) NOT NULL auto_increment,
  `groupname` varchar(50) NOT NULL default '',
  `notes` text NOT NULL,
  PRIMARY KEY  (`ID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.report_group...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `report_group_map`
--

CREATE TABLE `report_group_map` (
  `report_groupID` int(8) unsigned NOT NULL default '0',
  `server_moduleID` mediumint(8) unsigned NOT NULL default '0',
  KEY `report_groupID` (`report_groupID`),
  KEY `server_moduleID` (`server_moduleID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.report_group_map...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `role`
--

CREATE TABLE `role` (
  `ID` mediumint(8) NOT NULL auto_increment,
  `roleStringID` varchar(40) NOT NULL default '',
  `roleTypeID` mediumint(8) NOT NULL default '0',
  `roleDefinitionID` mediumint(8) NOT NULL default '0',
  `notes` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`ID`),
  KEY `roleStringID` (`roleStringID`),
  KEY `roleTypeID` (`roleTypeID`),
  KEY `notes` (`notes`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.role...\n";

$result=$dbb->prepare( $sql );
$result->execute;

$sql = << "END"
INSERT INTO `role` VALUES (1,'admin',1,1,'');

END
;

print "Filling in Table Data bigdaddy.role...\n";

$result=$dbb->prepare( $sql );
$result->execute;


#==============================================================================================
$sql = << "END"

--
-- Table structure for table `roleAction`
--

CREATE TABLE `roleAction` (
  `ID` mediumint(8) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL default '',
  `type` varchar(20) NOT NULL default '',
  `groupID` varchar(30) NOT NULL default '',
  PRIMARY KEY  (`ID`),
  UNIQUE KEY `type_2` (`type`,`groupID`),
  KEY `type` (`type`),
  KEY `groupID` (`groupID`)
) ENGINE=InnoDB;

END
;

print "Create Table bigdaddy.roleAction...\n";

$result=$dbb->prepare( $sql );
$result->execute;

print "Filling in Table Data bigdaddy.roleAction...\n";

$sql = "INSERT INTO `roleAction` VALUES (?,?,?,?)";
$result=$dbb->prepare( $sql );

$result->execute(1,'Manage Role System','manage-role-system','access');
$result->execute(2,'View User Admin','view-user-admin','access');
$result->execute(3,'Menu Admin','menu-admin','access');
$result->execute(4,'Edit Cron Entries','cron-edit','access');
$result->execute(5,'Delay Cron Entries','cron-delay','access');
$result->execute(6,'Globally Effect Cron Entries','cron-global','access');
$result->execute(7,'Administer Meta Info','meta-admin','access');
$result->execute(8,'Edit Server Entries','server-edit','access');
$result->execute(9,'Delay Server Entries','server-delay','access');
$result->execute(10,'Globally Effect Server Entries','server-global','access');
$result->execute(11,'Edit Incident Report','incident-edit','access');
$result->execute(12,'Admin Incident Reports','incident-admin','access');
$result->execute(13,'CRON Enable','cron-enable','access');
$result->execute(14,'CRON Disable','cron-disable','access');
$result->execute(15,'SERVER Enable','server-enable','access');
$result->execute(16,'SERVER Disable','server-disable','access');
$result->execute(17,'CRON Monitor Enable','cron-mon-enable','access');
$result->execute(18,'CRON Monitor Disable','cron-mon-disable','access');
$result->execute(19,'Server Meta Admin','server-meta','access');
$result->execute(20,'Crontab Meta Admin','cron-meta','access');
$result->execute(21,'SERVER Delete','server-remove','access');
$result->execute(22,'CRON Delete','cron-remove','access');


print "Creating Table bigdaddy.roleAction...\n";

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `roleDefinition`
--

CREATE TABLE `roleDefinition` (
  `ID` mediumint(8) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default '',
  `code` varchar(10) NOT NULL default '',
  `description` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`ID`),
  KEY `name` (`name`),
  KEY `code` (`code`),
  KEY `description` (`description`)
) ENGINE=InnoDB;

END
;


print "Creating Table bigdaddy.roleDefinition...\n";

$result=$dbb->prepare( $sql );
$result->execute;

$sql = << "END"

INSERT INTO `roleDefinition` VALUES (1,'Admin','access','Administrator Role');

END
;

print "Filling in Data  bigdaddy.roleDefinition...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `roleGlobalAction`
--

CREATE TABLE `roleGlobalAction` (
  `ID` int(10) NOT NULL auto_increment,
  `roleID` mediumint(8) unsigned NOT NULL default '0',
  `actionID` mediumint(8) unsigned NOT NULL default '0',
  PRIMARY KEY  (`ID`),
  KEY `roleID` (`roleID`),
  KEY `actionID` (`actionID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.roleGlobalAction...\n";

$result=$dbb->prepare( $sql );
$result->execute;

$sql="INSERT INTO `roleGlobalAction` VALUES (?,?,?)";
$result=$dbb->prepare( $sql );

$result->execute(1,1,1);
$result->execute(2,1,2);
$result->execute(3,1,3);
$result->execute(4,1,4);
$result->execute(5,1,5);
$result->execute(6,1,6);
$result->execute(7,1,7);
$result->execute(8,1,8);
$result->execute(9,1,9);
$result->execute(10,1,10);
$result->execute(11,1,11);
$result->execute(12,1,12);
$result->execute(13,1,13);
$result->execute(14,1,14);
$result->execute(15,1,15);
$result->execute(16,1,16);
$result->execute(17,1,17);
$result->execute(18,1,18);
$result->execute(19,1,19);
$result->execute(20,1,20);
$result->execute(21,1,21);
$result->execute(22,1,22);



#==============================================================================================
$sql = << "END"

--
-- Table structure for table `roleRelationship`
--

CREATE TABLE `roleRelationship` (
  `ID` int(10) NOT NULL auto_increment,
  `primaryRoleID` mediumint(8) unsigned NOT NULL default '0',
  `secondaryRoleID` mediumint(8) unsigned NOT NULL default '0',
  `actionID` mediumint(8) unsigned NOT NULL default '0',
  `objectID` int(10) unsigned NOT NULL default '0',
  `relationshipType` varchar(40) NOT NULL default '',
  PRIMARY KEY  (`ID`),
  KEY `primaryRoleID` (`primaryRoleID`),
  KEY `secondaryRoleID` (`secondaryRoleID`),
  KEY `primaryRoleID_2` (`primaryRoleID`,`actionID`),
  KEY `relationshipType` (`relationshipType`,`primaryRoleID`),
  KEY `relationshipType_2` (`relationshipType`,`objectID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.roleRelationship...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `roleType`
--

CREATE TABLE `roleType` (
  `ID` mediumint(8) NOT NULL auto_increment,
  `stringID` varchar(20) NOT NULL default '',
  `maxAllowed` smallint(6) NOT NULL default '0',
  `name` varchar(40) NOT NULL default '',
  `description` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`ID`),
  KEY `stringID` (`stringID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.roleType...\n";
$result=$dbb->prepare( $sql );
$result->execute;

$sql = << "END"
--
-- Dumping data for table `roleType`
--

INSERT INTO `roleType` VALUES (1,'admin',0,'Admin Roles','');

END
;

print "Filling in Data for bigdaddy.roleType...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `roleTypeRelationship`
--

CREATE TABLE `roleTypeRelationship` (
  `ID` int(10) NOT NULL auto_increment,
  `primaryRoleID` mediumint(8) unsigned NOT NULL default '0',
  `roleTypeID` mediumint(8) unsigned NOT NULL default '0',
  `actionID` mediumint(8) unsigned NOT NULL default '0',
  `relationshipType` varchar(40) NOT NULL default '',
  PRIMARY KEY  (`ID`),
  KEY `primaryRoleID` (`primaryRoleID`),
  KEY `roleTypeID` (`roleTypeID`),
  KEY `primaryRoleID_2` (`primaryRoleID`,`actionID`),
  KEY `relationshipType` (`relationshipType`,`primaryRoleID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.roleTypeRelationship...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `roleUser`
--

CREATE TABLE `roleUser` (
  `roleID` mediumint(8) NOT NULL default '0',
  `username` varchar(40) NOT NULL default '',
  `qualifier` varchar(100) NOT NULL default '',
  `startDate` datetime default '0000-00-00 00:00:00',
  `endDate` datetime default '0000-00-00 00:00:00',
  UNIQUE KEY `roleID_2` (`roleID`,`username`,`startDate`,`endDate`),
  KEY `username` (`username`),
  KEY `roleID` (`roleID`),
  KEY `roleID_3` (`roleID`,`qualifier`),
  KEY `username_2` (`username`,`qualifier`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.roleUser...\n";


$result=$dbb->prepare( $sql );
$result->execute;

$sql = "INSERT INTO `roleUser` VALUES (?,?,?,?,?)";
$result=$dbb->prepare( $sql );
$result->execute(1,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(2,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(3,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(4,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(5,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(6,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(7,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(8,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(9,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(10,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(11,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(12,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(13,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(14,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(15,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(16,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(17,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');
$result->execute(18,'admin','','0000-00-00 00:00:00','0000-00-00 00:00:00');


#==============================================================================================
$sql = << "END"

--
-- Table structure for table `server`
--

CREATE TABLE `server` (
  `ID` int(10) NOT NULL auto_increment,
  `iniID` varchar(40) NOT NULL default '',
  `hostname` varchar(100) NOT NULL default '',
  `active` tinyint(1) NOT NULL default '0',
  `delayed_until` int(10) unsigned NOT NULL default '0',
  `last_externalcheck` int(10) unsigned NOT NULL default '0',
  `last_internalcheck` int(10) unsigned NOT NULL default '0',
  `last_monenabled` int(10) unsigned NOT NULL default '0',
  `notes` text NOT NULL,
  `mon_startlevel` tinyint(1) unsigned NOT NULL default '0',
  `mon_currentlevel` tinyint(1) unsigned NOT NULL default '0',
  `mon_currentlevelstart` int(10) unsigned NOT NULL default '0',
  `mon_lastcheck` int(10) unsigned NOT NULL default '0',
  `mon_endlevel` tinyint(1) unsigned NOT NULL default '0',
  `mon_threshold` mediumint(8) unsigned NOT NULL default '0',
  `mon_internalreport` tinyint(1) unsigned NOT NULL default '0',
  `mon_externalreport` tinyint(1) unsigned NOT NULL default '0',
  `os_type` tinyint(1) unsigned NOT NULL default '0',
  PRIMARY KEY  (`ID`),
  KEY `active` (`active`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.server...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `server_category`
--

CREATE TABLE `server_category` (
  `ID` mediumint(8) unsigned NOT NULL auto_increment,
  `name` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`ID`)
) ENGINE=InnoDB;


END
;

print "Creating Table bigdaddy.server_category...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `server_category_map`
--

CREATE TABLE `server_category_map` (
  `serverID` int(8) unsigned NOT NULL default '0',
  `categoryID` mediumint(8) unsigned NOT NULL default '0',
  KEY `categoryID` (`categoryID`),
  KEY `serverID` (`serverID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.server_category_map...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"


--
-- Table structure for table `server_group`
--

CREATE TABLE `server_group` (
  `ID` int(10) NOT NULL auto_increment,
  `groupname` varchar(50) NOT NULL default '',
  `iniID` varchar(40) NOT NULL default '',
  `active` tinyint(1) NOT NULL default '0',
  `delayed_until` int(10) unsigned NOT NULL default '0',
  `last_monenabled` int(10) unsigned NOT NULL default '0',
  `notes` text NOT NULL,
  `mon_startlevel` tinyint(1) unsigned NOT NULL default '0',
  `mon_currentlevel` tinyint(1) unsigned NOT NULL default '0',
  `mon_currentlevelstart` int(10) unsigned NOT NULL default '0',
  `mon_lastcheck` int(10) unsigned NOT NULL default '0',
  `mon_endlevel` tinyint(1) unsigned NOT NULL default '0',
  `mon_percentthreshold` tinyint(2) unsigned NOT NULL default '0',
  `mon_numberthreshold` tinyint(2) unsigned NOT NULL default '0',
  PRIMARY KEY  (`ID`),
  KEY `active` (`active`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.server_group...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `server_group_map`
--

CREATE TABLE `server_group_map` (
  `server_groupID` int(8) unsigned NOT NULL default '0',
  `server_moduleID` mediumint(8) unsigned NOT NULL default '0',
  KEY `server_groupID` (`server_groupID`),
  KEY `server_moduleID` (`server_moduleID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.server_group_map...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `server_module`
--

CREATE TABLE `server_module` (
  `ID` mediumint(8) NOT NULL auto_increment,
  `serverID` int(10) unsigned NOT NULL default '0',
  `moduleID` int(10) unsigned NOT NULL default '0',
  `iniID` varchar(40) NOT NULL default '',
  `active` tinyint(1) unsigned NOT NULL default '0',
  `delayed_until` int(10) unsigned NOT NULL default '0',
  `mon_startlevel` tinyint(1) unsigned NOT NULL default '0',
  `mon_endlevel` tinyint(1) unsigned NOT NULL default '0',
  `mon_currentlevel` tinyint(1) unsigned NOT NULL default '0',
  `mon_currentlevelstart` int(10) unsigned NOT NULL default '0',
  `mon_lastcheck` int(10) unsigned NOT NULL default '0',
  `mon_lastreport` int(10) unsigned NOT NULL default '0',
  `mon_lastreportthreshold` mediumint(8) unsigned NOT NULL default '0',
  `last_monmodenabled` int(10) unsigned NOT NULL default '0',
  `lastreport_threshold` varchar(100) NOT NULL default '',
  `lastreport` varchar(100) NOT NULL default '',
  `parameters` varchar(150) NOT NULL default '',
  PRIMARY KEY  (`ID`),
  KEY `serverID` (`serverID`),
  KEY `moduleID` (`moduleID`),
  KEY `serverID_2` (`serverID`,`active`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.server...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"


--
-- Table structure for table `sessions`
--

CREATE TABLE `sessions` (
  `sessID` varchar(32) NOT NULL default '',
  `created` int(10) unsigned NOT NULL default '0',
  `value` text NOT NULL,
  PRIMARY KEY  (`sessID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.sessions...\n";


$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `treeEntry`
--

CREATE TABLE `treeEntry` (
  `ID` mediumint(8) unsigned NOT NULL auto_increment,
  `parentID` mediumint(8) unsigned NOT NULL default '0',
  `name` varchar(100) NOT NULL,
  `link` varchar(255) NOT NULL,
  `active` tinyint(1) NOT NULL default '0',
  `roleID` text NOT NULL,
  `userID` text NOT NULL,
  `projectID` text NOT NULL,
  `deptID` text NOT NULL,
  `groupName` text NOT NULL,
  `ord` smallint(5) unsigned NOT NULL default '0',
  `actionID` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`ID`),
  KEY `parentID` (`parentID`),
  KEY `ord` (`ord`)
) ENGINE=InnoDB;

END
;


print "Creating Table bigdaddy.treeEntry...\n";


$result=$dbb->prepare( $sql );
$result->execute;

$sql = "INSERT INTO `treeEntry` VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";

$result=$dbb->prepare( $sql );

$result->execute(7,6,'INI Files','/index.html?page=MonIniFiles',0,'','','','','',1,0);
$result->execute(3,1,'Server Control','/index.html?page=ServerIndex',0,'','','','','',0,0);
$result->execute(9,0,'Site Admin','#',0,'1','','','','',3,0);
$result->execute(10,9,'Role Administration','/admin/role/',0,'1','','','','',0,0);
$result->execute(6,0,'BigDaddy Admin','#',0,'','','','','',2,0);
$result->execute(16,5,'Reporting Groups','/?page=ReportGroupIndex',0,'','','','','',1,0);
$result->execute(15,5,'Incident Reports','/incident/index.html',0,'','','','','',0,0);
$result->execute(5,0,'Reports','#',0,'','','','','',1,0);
$result->execute(14,6,'Phone/Email Tags','/index.html?page=PhoneTags',0,'','','','','',2,0);
$result->execute(8,6,'Monitoring Modules','/index.html?page=MonModules',0,'','','','','',0,0);
$result->execute(12,9,'User Admin','/admin/user/',0,'1','','','','',1,0);
$result->execute(11,9,'Menu Admin','/admin/treeview/',0,'1','','','','',2,0);
$result->execute(1,0,'Monitoring','#',0,'','','','','',0,0);
$result->execute(2,1,'Crontab Control','/index.html?page=CrontabIndex',0,'','','','','',1,0);
$result->execute(4,1,'Server Group Control','/index.html?page=ServerGroupIndex',0,'','','','','',2,0);

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `inifile`
--

CREATE TABLE `inifile` (
  `ID` varchar(40) NOT NULL default '',
  `name` varchar(150) NOT NULL default '',
  `inifile` text NOT NULL,
  PRIMARY KEY  (`ID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.inifile...\n";


$result=$dbb->prepare( $sql );
$result->execute;

$sql = "INSERT INTO `inifile` VALUES ('incident','Incident Reporting (Global)','[email]\r\nincidentadd=tag1,tag2\r\ncommentadd=tag1,tag2'),('crontab','Application Monitoring (Global)','[global]\r\nglobaldelay=0\r\nglobalstatus=on\r\n'),('servermon','Server Monitoring (Global)','[global]\r\nglobaldelay=0\r\nglobalstatus=on'),('escalate-default','Escalate (Sample)','[global]\r\nescalate=yes\r\nusetags=yes\r\n\r\n[level1]\r\nemail=tag1,tag2\r\nmessage=\r\nescalate=1200\r\nloop=600\r\n\r\n[level2]\r\nemail=tag1,tag2\r\nmessage=\r\nescalate=1200\r\nloop=600\r\n\r\n[level3]\r\nemail=tag1,tag2\r\nmessage=\r\nescalate=1200\r\nloop=600\r\n\r\n[level4]\r\nemail=tag1,tag2\r\nmessage=\r\nescalate=1200\r\nloop=600\r\n\r\n[level5]\r\nemail=tag1,tag2\r\nmessage=CRITICAL\r\nloop=600'),('escalate-variable-delay','Escalate (Variable Delay) (Sample)','[global]\r\nescalate=yes\r\ntempdelaystart=2:50\r\ntempdelayend=4:10\r\nusetags=yes\r\n\r\n[level1]\r\nemail=tag1,tag2\r\nmessage=\r\nescalate=1200\r\nloop=600\r\n\r\n[level2]\r\nemail=tag1,tag2\r\nmessage=\r\nescalate=1200\r\nloop=600\r\n\r\n[level3]\r\nemail=tag1,tag2\r\nmessage=\r\nescalate=1200\r\nloop=600\r\n\r\n[level4]\r\nemail=tag1,tag2\r\nmessage=\r\nescalate=1200\r\nloop=600\r\n\r\n[level5]\r\nemail=tag1,tag2\r\nmessage=CRITICAL\r\nloop=600')";

print "Filling in Data for bigdaddy.inifile...\n";

$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

CREATE TABLE `module` (
  `ID` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(150) NOT NULL default '',
  `filename` varchar(150) NOT NULL default '',
  `type` tinyint(1) unsigned NOT NULL default '0',
  `runfrom` tinyint(1) unsigned NOT NULL default '0',
  `distribute` tinyint(1) unsigned NOT NULL default '0',
  `notes` text NOT NULL,
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=33 DEFAULT CHARSET=latin1;

END
;

print "Creating Table bigdaddy.module...\n";


$result=$dbb->prepare( $sql );
$result->execute;

$sql = "INSERT INTO `module` VALUES (26,'Win CPU Usage','snmp_wincpu.pl',2,1,0,'Windows CPU %'),(29,'RAM Usage (Linux)','mod_memusage.pl',2,1,1,''),(20,'Win Disk Space','snmp_winhdcheck.pl',2,1,0,'Checks windows servers running SNMP for free hard disk space. Usage:\r\n\r\nsnmp_winhdcheck.pl &lt;servername&gt; &lt;driveletter&gt;'),(15,'DiskSpace Used','mod_diskspace.pl',2,1,0,''),(31,'Process Check','snmp_pscheck.pl',1,1,0,''),(27,'Win RAM Usage','snmp_winram.pl',2,1,0,'Windows RAM Usage %'),(14,'Swap Used','mod_swapused.pl',2,1,1,''),(12,'Load Average','mod_loadavg.pl',2,1,0,''),(25,'TCP Port','mod_port_tcp.pl',1,1,0,'Monitor a TCP Port\r\n\r\nArguments:\r\n\r\n0 - [hostname]\r\n1 - TCP Port #'),(2,'Ping (ICMP)','mod_ping.pl',1,1,0,'')";

print "Filling in Data for bigdaddy....\n";

$result=$dbb->prepare( $sql );
$result->execute;

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `userRelationship`
--

CREATE TABLE `userRelationship` (
  `ID` int(10) NOT NULL auto_increment,
  `primaryUsername` varchar(40) NOT NULL,
  `secondaryUsername` varchar(40) default NULL,
  `actionID` mediumint(8) unsigned NOT NULL default '0',
  `objectID` int(10) unsigned NOT NULL default '0',
  `relationshipType` varchar(40) NOT NULL default '',
  PRIMARY KEY  (`ID`),
  KEY `primaryUsername` (`primaryUsername`),
  KEY `secondaryUsername` (`secondaryUsername`),
  KEY `primaryUsername_2` (`primaryUsername`,`actionID`),
  KEY `relationshipType` (`relationshipType`,`primaryUsername`),
  KEY `relationshipType_2` (`relationshipType`,`objectID`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.userRelationship...\n";


$result=$dbb->prepare( $sql );
$result->execute;


my $password = "";
my $pass_check = "false";

while ($pass_check eq 'false')
{
    print "\n\nChoose a password for user 'admin' > ";
    chomp (my $password_1 = <>);
    print "\nRe-Enter password > ";
    chomp (my $password_2 = <>);
    
    if ($password_1 eq $password_2)
    {
        $password = $password_1;
        $pass_check = "true";
    }
    else
    {
        print "\nThe Passwords did not match, please try again.\n";
    }
}
    

#==============================================================================================
$sql = << "END"

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `username` varchar(40) NOT NULL,
  `password` varchar(32) NOT NULL default '',
  `firstname` varchar(100) NOT NULL default '',
  `lastname` varchar(100) NOT NULL default '',
  `cn` varchar(100) NOT NULL default '',
  `fullname` varchar(100) NOT NULL default '',
  `active` tinyint(1) NOT NULL default '0',
  `position` varchar(100) NOT NULL default '',
  `location` varchar(100) NOT NULL default '',
  `lastActive` datetime NOT NULL,
  `lastLogin` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`username`),
  KEY `active` (`active`)
) ENGINE=InnoDB;

END
;

print "Creating Table bigdaddy.users... with username 'admin' and password '$password'...\n";

$result=$dbb->prepare( $sql );
$result->execute;

$sql = "INSERT INTO `users` VALUES ('admin',MD5('$password'),'Super','Admin','','Super Admin',1,'','','',0)";

print "Adding username 'admin' and password '$password'...\n";

$result=$dbb->do( $sql );

#==============================================================================================
$sql = << "END"
--
-- Table structure for table `auditData`
--

CREATE TABLE `auditData` (
  `ID` int(10) NOT NULL auto_increment,
  `application` varchar(100) NOT NULL default '',
  `modifiedDate` datetime default NULL,
  `userID` int(10) NOT NULL default '0',
  `userIDAffected` int(10) NOT NULL default '0',
  `username` varchar(40) NOT NULL default '',
  `usernameAffected` varchar(40) NOT NULL default '',
  `primaryNumericID` int(10) unsigned NOT NULL default '0',
  `secondaryNumericID` int(10) unsigned NOT NULL default '0',
  `primaryStringID` varchar(255) NOT NULL default '',
  `secondaryStringID` varchar(255) NOT NULL default '',
  `type` varchar(80) NOT NULL default '',
  `action` varchar(80) NOT NULL default '',
  `comment` text,
  PRIMARY KEY  (`ID`),
  KEY `application` (`application`),
  KEY `username` (`username`),
  KEY `usernameAffected` (`usernameAffected`),
  KEY `primaryNumericID` (`primaryNumericID`),
  KEY `primaryStringID` (`primaryStringID`),
  KEY `type` (`type`),
  KEY `action` (`action`),
  KEY `modifiedDate` (`modifiedDate`),
  KEY `userID` (`userID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

END
;

print "Creating Table bigdaddystats.auditData...\n";

$result=$dbs->prepare( $sql );
$result->execute;

$sql = << "END"

--
-- Table structure for table `bool15_server_module_history`
--

CREATE TABLE `bool15_server_module_history` (
  `serverModuleID` int(10) unsigned NOT NULL default '0',
  `serverID` int(10) unsigned NOT NULL default '0',
  `moduleID` int(10) unsigned NOT NULL default '0',
  `mon_lastreport` int(10) unsigned NOT NULL default '0',
  `lastreport` varchar(100) NOT NULL default '',
  KEY `serverID` (`serverID`),
  KEY `serverID_2` (`serverID`,`moduleID`,`lastreport`),
  KEY `serverModuleID` (`serverModuleID`,`lastreport`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

END
;

print "Creating Table bigdaddystats.bool15_server_module_history...\n";

$result=$dbs->prepare( $sql );
$result->execute;

$sql = << "END"

--
-- Table structure for table `bool180_server_module_history`
--

CREATE TABLE `bool180_server_module_history` (
  `serverModuleID` int(10) unsigned NOT NULL default '0',
  `serverID` int(10) unsigned NOT NULL default '0',
  `moduleID` int(10) unsigned NOT NULL default '0',
  `mon_lastreport` int(10) unsigned NOT NULL default '0',
  `lastreport` varchar(100) NOT NULL default '',
  KEY `serverID` (`serverID`),
  KEY `serverID_2` (`serverID`,`moduleID`,`lastreport`),
  KEY `serverModuleID` (`serverModuleID`,`lastreport`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

END
;

print "Creating Table bigdaddystats.bool180_server_module_history...\n";

$result=$dbs->prepare( $sql );
$result->execute;

$sql = << "END"

--
-- Table structure for table `bool60_server_module_history`
--

CREATE TABLE `bool60_server_module_history` (
  `serverModuleID` int(10) unsigned NOT NULL default '0',
  `serverID` int(10) unsigned NOT NULL default '0',
  `moduleID` int(10) unsigned NOT NULL default '0',
  `mon_lastreport` int(10) unsigned NOT NULL default '0',
  `lastreport` varchar(100) NOT NULL default '',
  KEY `serverID` (`serverID`),
  KEY `serverID_2` (`serverID`,`moduleID`,`lastreport`),
  KEY `serverModuleID` (`serverModuleID`,`lastreport`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

END
;

print "Creating Table bigdaddystats.bool60_server_module_history...\n";

$result=$dbs->prepare( $sql );
$result->execute;

$sql = << "END"

--
-- Table structure for table `cronHistory`
--

CREATE TABLE `cronHistory` (
  `cronID` int(8) unsigned NOT NULL default '0',
  `last_ranstart` int(10) unsigned NOT NULL default '0',
  `last_ranend` int(10) unsigned NOT NULL default '0',
  `time_elapsed` int(10) unsigned NOT NULL default '0',
  KEY `cronID` (`cronID`),
  KEY `cronID_2` (`cronID`,`last_ranend`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

END
;

print "Creating Table bigdaddystats.cronHistory...\n";

$result=$dbs->prepare( $sql );
$result->execute;

$sql = << "END"

--
-- Table structure for table `cronLog`
--

CREATE TABLE `cronLog` (
  `cronID` int(8) unsigned NOT NULL default '0',
  `logEntryTimestamp` int(10) unsigned NOT NULL default '0',
  `logEntry` text NOT NULL,
  KEY `cronID` (`cronID`),
  KEY `cronID_2` (`cronID`,`logEntryTimestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

END
;

print "Creating Table bigdaddystats.cronLog...\n";

$result=$dbs->prepare( $sql );
$result->execute;

$sql = << "END"

--
-- Table structure for table `ma15_server_module_history`
--

CREATE TABLE `ma15_server_module_history` (
  `serverID` int(10) unsigned NOT NULL default '0',
  `moduleID` int(10) unsigned NOT NULL default '0',
  `mon_lastreport` int(10) unsigned NOT NULL default '0',
  `lastreport` varchar(100) NOT NULL default '',
  `serverModuleID` int(10) unsigned NOT NULL default '0',
  KEY `serverID` (`serverID`),
  KEY `serverModuleID_2` (`serverModuleID`,`mon_lastreport`),
  KEY `mon_lastreport` (`mon_lastreport`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

END
;

print "Creating Table bigdaddystats.ma15_server_module_history...\n";

$result=$dbs->prepare( $sql );
$result->execute;

$sql = << "END"

--
-- Table structure for table `ma180_server_module_history`
--

CREATE TABLE `ma180_server_module_history` (
  `serverID` int(10) unsigned NOT NULL default '0',
  `moduleID` int(10) unsigned NOT NULL default '0',
  `mon_lastreport` int(10) unsigned NOT NULL default '0',
  `lastreport` varchar(100) NOT NULL default '',
  `serverModuleID` int(10) unsigned NOT NULL default '0',
  KEY `serverID` (`serverID`),
  KEY `serverModuleID_2` (`serverModuleID`,`mon_lastreport`),
  KEY `mon_lastreport` (`mon_lastreport`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

END
;

print "Creating Table bigdaddystats.ma180_server_module_history...\n";

$result=$dbs->prepare( $sql );
$result->execute;

$sql = << "END"

--
-- Table structure for table `ma60_server_module_history`
--

CREATE TABLE `ma60_server_module_history` (
  `serverID` int(10) unsigned NOT NULL default '0',
  `moduleID` int(10) unsigned NOT NULL default '0',
  `mon_lastreport` int(10) unsigned NOT NULL default '0',
  `lastreport` varchar(100) NOT NULL default '',
  `serverModuleID` int(10) unsigned NOT NULL default '0',
  KEY `serverID` (`serverID`),
  KEY `serverModuleID_2` (`serverModuleID`,`mon_lastreport`),
  KEY `mon_lastreport` (`mon_lastreport`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

END
;

print "Creating Table bigdaddystats.ma60_server_module_history...\n";

$result=$dbs->prepare( $sql );
$result->execute;

$sql = << "END"

--
-- Table structure for table `ma_cronstatus`
--

CREATE TABLE `ma_cronstatus` (
  `ma_type` varchar(20) NOT NULL default '',
  `last_end` int(10) unsigned NOT NULL default '0',
  KEY `ma_type` (`ma_type`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

END
;

print "Creating Table bigdaddystats.ma_cronstatus...\n";

$result=$dbs->prepare( $sql );
$result->execute;

$sql = << "END"


--
-- Table structure for table `nonbool_server_module_history`
--

CREATE TABLE `nonbool_server_module_history` (
  `serverModuleID` int(10) unsigned NOT NULL default '0',
  `serverID` int(10) unsigned NOT NULL default '0',
  `moduleID` int(10) unsigned NOT NULL default '0',
  `monHourStamp` int(10) unsigned NOT NULL default '0',
  `lastreportData` text NOT NULL,
  KEY `serverID` (`serverID`),
  KEY `serverID_2` (`serverID`,`moduleID`),
  KEY `serverModuleID` (`serverModuleID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

END
;

print "Creating Table bigdaddystats.nonbool_server_module_history...\n";

$result=$dbs->prepare( $sql );
$result->execute;

$sql = << "END"

--
-- Table structure for table `server_module_history`
--

CREATE TABLE `server_module_history` (
  `serverModuleID` int(10) unsigned NOT NULL default '0',
  `serverID` int(10) unsigned NOT NULL default '0',
  `moduleID` int(10) unsigned NOT NULL default '0',
  `mon_lastreport` int(10) unsigned NOT NULL default '0',
  `lastreport` varchar(100) NOT NULL default '',
  KEY `serverID` (`serverID`),
  KEY `serverModuleID` (`serverModuleID`,`lastreport`),
  KEY `mon_lastreport` (`mon_lastreport`),
  KEY `serverModuleID_2` (`serverModuleID`,`mon_lastreport`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

END
;

print "Creating Table bigdaddystats.server_module_history...\n";

$result=$dbs->prepare( $sql );
$result->execute;
